/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.problemas;

import com.actions.cambios.ReporteCambioPDF;
import com.itextpdf.text.DocumentException;
import com.mapping.CambioComp;
import com.mapping.Componentes;
import com.mapping.Dispositivo;
import com.mapping.Ticket;
import com.opensymphony.xwork2.ActionSupport;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
/**
 *
 * @author Andres
 */
@Namespace(value = "/")
@Action(value = "prueba2pdf", results = {@Result(name = "success", location = "/index.jsp")})
public class Prueba2Action extends ActionSupport implements ServletRequestAware{
    private String idTicket;
    public HttpServletRequest request;
    @Override
    public String execute(){
        idTicket = "TI9101554";
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
            Ticket ticket = em.getReference(Ticket.class, idTicket);
        tx.commit();
        em.close();
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date date = new Date();
        String ruta = request.getSession().getServletContext().getRealPath("/")+"\\ReportesSoluciones";
        String nombreArchivo = "prueba.pdf";
        String fecha = dateFormat.format(date);
        ReporteSolucionPDF reporte = new ReporteSolucionPDF(ticket, ticket.getIdDispositivo(),ticket.getIdSolucion(), 
                fecha, ruta, nombreArchivo);
        try {    
            reporte.generar();
        } catch (DocumentException | IOException de) {
            ruta = de.getMessage();
        }
        
        return SUCCESS;
    }    
    
    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;  
    }
}
